package com.yanqu.road.dao.impl.nenmonopolymarket;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.NewMonopolyMarketNoticeData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewMonopolyMarketNoticeDataDaoImpl extends TempDataObjectDao<NewMonopolyMarketNoticeData> {


    public static String getNewMonopolyMarketNoticeDataSql = "select * from `t_c_new_monopoly_market_notice` where `weekId`=? and `groupId`=? and `time`>?  ORDER BY `time`";
    public Map<Integer, List<NewMonopolyMarketNoticeData>> getNewMonopolyMarketNoticeData(int weekId, long groupId, int time) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<NewMonopolyMarketNoticeData>> map = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getNewMonopolyMarketNoticeDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            call.setLong(i++, groupId);
            call.setInt(i++, time);
            rs = call.executeQuery();
            while (rs.next()) {
                NewMonopolyMarketNoticeData data = parse(rs);
                if(!map.containsKey(data.getResGroupId())){
                    map.put(data.getResGroupId(), new ArrayList<>());
                }
                map.get(data.getResGroupId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateNewMonopolyMarketNoticeDataSql = "update `t_c_new_monopoly_market_notice` set userId=?,pos=?,robedId=?,actionType=?,time=? where weekId=? and groupId=? and resGroupId=? and type=?";
    @Override
    protected boolean updateData(NewMonopolyMarketNoticeData newMonopolyMarketNoticeData) {
        newMonopolyMarketNoticeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketNoticeData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketNoticeData.getPos()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketNoticeData.getRobedId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketNoticeData.getActionType()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketNoticeData.getTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketNoticeData.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketNoticeData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketNoticeData.getResGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketNoticeData.getType()));
        if (getDb().executeNoneQuery(updateNewMonopolyMarketNoticeDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertNewMonopolyMarketNoticeDataSql = "insert into `t_c_new_monopoly_market_notice`(weekId,groupId,resGroupId,type,userId,pos,robedId,actionType,occupyType,time) values (?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(NewMonopolyMarketNoticeData newMonopolyMarketNoticeData) {
        newMonopolyMarketNoticeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketNoticeData.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketNoticeData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketNoticeData.getResGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketNoticeData.getType()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketNoticeData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketNoticeData.getPos()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketNoticeData.getRobedId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketNoticeData.getActionType()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketNoticeData.getOccupyType()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketNoticeData.getTime()));
        if (getDb().executeNoneQuery(insertNewMonopolyMarketNoticeDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected NewMonopolyMarketNoticeData parseData(ResultSet rs) throws SQLException {
        NewMonopolyMarketNoticeData newMonopolyMarketNoticeData = new NewMonopolyMarketNoticeData();
        newMonopolyMarketNoticeData.setWeekId(rs.getInt("weekId"));
        newMonopolyMarketNoticeData.setGroupId(rs.getLong("groupId"));
        newMonopolyMarketNoticeData.setResGroupId(rs.getInt("resGroupId"));
        newMonopolyMarketNoticeData.setType(rs.getInt("type"));
        newMonopolyMarketNoticeData.setUserId(rs.getLong("userId"));
        newMonopolyMarketNoticeData.setPos(rs.getInt("pos"));
        newMonopolyMarketNoticeData.setRobedId(rs.getLong("robedId"));
        newMonopolyMarketNoticeData.setActionType(rs.getInt("actionType"));
        newMonopolyMarketNoticeData.setOccupyType(rs.getInt("occupyType"));
        newMonopolyMarketNoticeData.setTime(rs.getInt("time"));

        return newMonopolyMarketNoticeData;
    }
}
